package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.ContactName;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by miroshnik on 1/23/2015.
 */
public class ContactNameDao extends AbstractDao<ContactName> {
    
    private static final String ENTITY_NAME = ContactName.class.getSimpleName();

    private static final String SELECT_BY_LEGAL_PERSON_ID = "select_by_legal_person_id";
    private static final String CONTACT_ID = "CONTACT_ID";
    private static final String LEGAL_PERSON_ID = "LEGAL_PERSON_ID";
    private static final String FULL_NAME = "FULL_NAME";
    private static final String TEL = "TEL";
    private static final String EMAIL = "EMAIL";

    public ContactNameDao() {
        this(ENTITY_NAME);
    }

    public ContactNameDao(String entityName) {
        super(entityName);
    }
    
    public ContactNameDao(Connection connection) throws SQLException {
        super(connection);
    }

    public ContactNameDao(Connection connection, String entityName) throws SQLException {
        super(connection, entityName);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, ContactName object) throws SQLException {
        statement.setInt(1, object.getPersonId());
        statement.setString(2, object.getEmail());
        statement.registerOutParameter(3, Types.NUMERIC);
        statement.setString(4, object.getFullName());
        statement.setString(5, object.getTel());
        statement.setString(6, object.getEmail());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, ContactName object) throws SQLException {
        statement.setInt(1, object.getId());
        statement.setString(2, object.getEmail());
        statement.setInt(3, object.getPersonId());
        statement.setString(4, object.getFullName());
        statement.setString(5, object.getTel());
        statement.setString(6, object.getEmail());
    }

    @Override
    protected List<ContactName> parseResultSet(ResultSet rs) throws SQLException {
        List<ContactName> contacts = new ArrayList<>();

        while (rs.next()) {
            ContactName contact = new ContactName(rs.getInt(CONTACT_ID), rs.getInt(LEGAL_PERSON_ID));
            contact.setEmail(rs.getString(EMAIL));
            contact.setFullName(rs.getString(FULL_NAME));
            contact.setTel(rs.getString(TEL));
            contacts.add(contact);
        }

        return contacts;
    }

    @Override
    public int create(ContactName obj) throws SQLException {
        return super.create(obj, 3);
    }

    public ContactName readByLegalPersonId(int id) throws SQLException {
        String query = queryManager.getQuery(entityName, SELECT_BY_LEGAL_PERSON_ID);
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            List<ContactName> result = parseResultSet(rs);

            return result.size() > 0 ? result.get(0) : null;
        }
    }
}
